package com.laiketui.online.message.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;
/**
 * 线程池配置类
 */
@Configuration
public class ThreadPoolConfig {
    @Bean("asyncUploadExcelExecutor")
    public ThreadPoolTaskExecutor asyncUploadExcelExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor =  new ThreadPoolTaskExecutor();
        // 设置核心线程数
        threadPoolTaskExecutor.setCorePoolSize(4);
        // 设置最大线程数
        threadPoolTaskExecutor.setMaxPoolSize(8);
        // 设置阻塞队列大小
        threadPoolTaskExecutor.setQueueCapacity(999);
        // 默认是 60s，这里设置30s
        threadPoolTaskExecutor.setKeepAliveSeconds(30);
        // 设置线程池中线程名前缀
        threadPoolTaskExecutor.setThreadNamePrefix("async-upload-excel");
        //当达到 MaxPoolSize 不再调用新线程，用调用者所在线程之星异步任务。
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return threadPoolTaskExecutor;
    }

}
